package com.edu.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.edu.config.exceptionHandler.exception.GuliException;
import com.edu.entity.SubjectData;
import com.edu.entity.excel.ExcelSubjectData;
import com.edu.service.SubjectService;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.Map;

/**
 * @author mxs
 * 读取subject Excel文件的监听类
 * 定义具体如何操作读取的数据
 * 由于该类不能交给spring容器管理，所以无法通过注入的方式创建Service对象
 * 所以需要通过构造方法擦传入service
 */
@AllArgsConstructor
@NoArgsConstructor
@Data
public class ExcelSubjectExcelListener extends AnalysisEventListener<ExcelSubjectData> {
    public SubjectService subjectService;

    /**
     * 读取文件信息并且保存到数据库
     *
     * @param excelSubjectData
     * @param analysisContext
     */
    @Override
    public void invoke(ExcelSubjectData excelSubjectData, AnalysisContext analysisContext) {
        //先判断文件是否为空
        if (excelSubjectData == null) {
            throw new GuliException(20001, "文件为空");
        }
        //判断一级名称是否为空
        SubjectData one = this.existOneSubjectName(subjectService, excelSubjectData.getOneSubjectName());
        if (one == null) {
            //如果为空就创建一个新的SubObjectData对象
            one = new SubjectData();
            one.setParentId("0");
            one.setTitle(excelSubjectData.getOneSubjectName());
            subjectService.save(one);
        }
        String pid = one.getId();
        //判断第二级元素是否为空
        SubjectData two = this.existTwoSubjectName(subjectService, one.getTitle(), pid);
        if (two == null) {
            two = new SubjectData();
            two.setTitle(excelSubjectData.getTwoSubjectName());
            two.setParentId(pid);
            subjectService.save(two);
        }

    }

    /**
     * 读取表头信息
     *
     * @param headMap
     * @param context
     */
    @Override
    public void invokeHeadMap(Map<Integer, String> headMap, AnalysisContext context) {
        System.out.println("表头信息为:" + headMap);
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {

    }

    /**
     * 判断第一级元素是否重复
     *
     * @param subjectService
     * @param name           一级教程的名称
     * @return
     */
    private SubjectData existOneSubjectName(SubjectService subjectService, String name) {
        QueryWrapper<SubjectData> subjectQueryWrapper = new QueryWrapper<>();
        subjectQueryWrapper.eq("parent_id", 0);
        subjectQueryWrapper.eq("title", name);
        SubjectData subject = subjectService.getOne(subjectQueryWrapper);
        return subject;
    }

    /**
     * 判断第二级元素是否存在
     *
     * @param subjectService
     * @param title          二级教程名称
     * @param parent_id      父元素的id(如果是一级元素父元素的id就为0)
     * @return
     */
    private SubjectData existTwoSubjectName(SubjectService subjectService, String title, String parent_id) {
        QueryWrapper<SubjectData> subjectQueryWrapper = new QueryWrapper<>();
        subjectQueryWrapper.eq("parent_id", parent_id);
        subjectQueryWrapper.eq("title", title);
        SubjectData subject = subjectService.getOne(subjectQueryWrapper);
        return subject;
    }
}
